jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.timeComparison.nodeStats.NodeStats", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.nodeTimeComparisonCacheId = "SPM_NODE_TIMECOMPARISON_TABLE_CACHE";
        	plugin.nodeTimeCompStartedCacheId = "SPM_NODE_TIMECOMPARISON_STARTED_TABLE_CACHE";
        	
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        
        	plugin.prototype.nodeId;
        	plugin.prototype.isWaitingForResponse = false;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.setUpComponent = function() {
                this.isWaitingForResponse = false;
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
            
            	var subject = tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.nodeInfoCacheSubject;
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(subject, this.retrieveData, this);
            }
            
            plugin.prototype.timeframeSelected = function(subject, message, tableObj) {
            	var timeframeId = message.selectedTimeframe;
            	tableObj.callGetNodes(tableObj, timeframeId, false);
            }
            
            plugin.prototype.retrieveData = function(subject, message, nodeStatsObj) {
            	nodeStatsObj.callGetNodes(nodeStatsObj, "sincestarted", true);
            }
            
            plugin.prototype.loadTable = function(nodeStatsObj) {
            	var server = tibco.admin.Main.getServer();
            	
           		var generalCachedDoc = server.Cache.getDocument(plugin.nodeTimeCompStartedCacheId);
           		var timeframeCachedDoc = server.Cache.getDocument(plugin.nodeTimeComparisonCacheId);
           		
            	var timeframeRecord = timeframeCachedDoc.getFirstChild();
            	var generalRecord = generalCachedDoc.getFirstChild();
            	
            	var tableDoc = jsx3.xml.CDF.Document.newDocument();
		var availabilityText = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@nodesLblAvailability");
		var faultsText = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@nodesLblFaults");
		var successesText = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@nodesLblSuccesses");
            	var recordAttrs = plugin.createRecordAttrs("availablilityId", availabilityText, timeframeRecord.getAttribute("availability"), generalRecord.getAttribute("availability"));
            	tableDoc.insertRecord(recordAttrs);
            	recordAttrs = plugin.createRecordAttrs("faultsId", faultsText, timeframeRecord.getAttribute("faults"), generalRecord.getAttribute("faults"));
            	tableDoc.insertRecord(recordAttrs);
            	recordAttrs = plugin.createRecordAttrs("successesId", successesText, timeframeRecord.getAttribute("successes"), generalRecord.getAttribute("successes"));
            	tableDoc.insertRecord(recordAttrs);
            	
      			server.Cache.setDocument(plugin.nodeTimeComparisonCacheId, tableDoc);
      			
      			var tableObj = nodeStatsObj.getDescendantOfName("nodeStatsTable_MM_infra");
      			
      			// set the timeframe column header name
      			var timeframeCol = tableObj.getDescendantOfName("timeframe1Col");
      			var timeframeName = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeName();
      			timeframeCol.setText(timeframeName, true);
      			
      			tableObj.repaintData();
            }
            
            plugin.createRecordAttrs = function(jsxidVal, header, data1, data2) {
            	var recordAttrs = new Object();
            	recordAttrs.jsxid = jsxidVal;
            	recordAttrs.header = header;
            	recordAttrs.dataCol1 = data1;
            	recordAttrs.dataCol2 = data2;
            	return recordAttrs;
            }
            
            //call this method to begin the service call (eg.service.callgetNodes();)
    		plugin.prototype.callGetNodes = function(tableObj, timeframeId, isSinceStarted) {
    			var server = tibco.admin.Main.getServer();
    			var rulesURI = "tibco/admin/monitormanage/uicomponents/table/timeComparison/nodeStats/rules/nodeRules.xml";
    			var dataURI = "tibco/admin/monitormanage/uicomponents/table/timeComparison/nodeStats/data/nodeData.xml";
    			if(isSinceStarted)
    			{
	    			rulesURI = "tibco/admin/monitormanage/uicomponents/table/timeComparison/nodeStats/rules/nodeSinceStartRules.xml";
	    			dataURI = "tibco/admin/monitormanage/uicomponents/table/timeComparison/nodeStats/data/nodeSinceStartData.xml";
	    			tableObj.isWaitingForResponse = true;
    			}
    			if (server.getEnv("MODE")) {
                    // mode is live
                    var objService = tibco.admin.Main.loadService(tableObj.getNamespace(), rulesURI, "getNodes");
                    objService.setEndpointURL(plugin.wsEndpointURL);
		    		// this setInboundURL should not be required...only here because of a current AMX framework bug
		    		objService.setInboundURL(dataURI);
                }
                else {
                    var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(tableObj.getNamespace(), rulesURI), "getNodes", null, null);
                    // mode is static
                    objService.setInboundURL(tibco.admin.Main.resolveURI(tableObj.getNamespace(), dataURI));
                }
                
                objService.nodeStatsObj = tableObj;
                objService.isSinceStarted = isSinceStarted;
                
                // initialize the node id
                var cacheId = tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.nodeScreenCacheId;
           		var generalCachedDoc = server.Cache.getDocument(cacheId);
           		var nodeId = generalCachedDoc.getFirstChild().getAttribute("key");
                objService.nodeId = nodeId;
                jsx3.log("NodeStats.js: node id = " + nodeId);
                
                if(!timeframeId)
				{ timeframeId = tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.getCurrentTimeframeId(); }
				objService.timeframeId = timeframeId;
                
                // initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("availability");
				metricsList.add("successes");
				metricsList.add("faults");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, tableObj.ongetNodesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, tableObj.ongetNodesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, tableObj.ongetNodesInvalid);
      			objService.doCall();
      			
      			if(isSinceStarted)
      			{ tableObj.callGetNodes(tableObj, null, false); }
    		};

    		plugin.prototype.ongetNodesSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			var nodeStatsObj = objEvent.target.nodeStatsObj;
      			if(nodeStatsObj.isWaitingForResponse)
      			{ nodeStatsObj.isWaitingForResponse = false; }
      			else
      			{ nodeStatsObj.loadTable(nodeStatsObj); }
    		};

    		plugin.prototype.ongetNodesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetNodesInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};

        });
